#pragma once
#include "buffer.h"
#include "type_enum.h"
#include "logheader.h"
#include "event.h"
#ifndef BINLOG_UTIL_H
#define BINLOG_UTIL_H
typedef struct bin_util {
private :
	PTL_BUFFER buffer = new TL_BUFFER();
	TL_HEADER header;
	STRING log_file;
public:
	bin_util() {}
	bin_util(STRING log_file) { this->log_file = log_file; }
	~bin_util() {
		buffer->~buffer_struct();

	}
public:
	
	bin_util * set_log_file(STRING log_file) {
		this->log_file = log_file;
		return this;
	}
	bin_util * analyze() {
		try {
			CHECK_BINLOG(buffer->openfile(this->log_file.c_str()));
		}
		catch (BLEXCEPTION e) {
			ERROR(e.what());
		}
		while (buffer->next()) {
			header.encode(buffer);
			TL_LOGTYPE log_type=TL_LOGTYPE(header.get_type_code());
			CPTR type_message=type_chang_to_name(log_type);	
			printf("%X \n", buffer->get_position());
			switch (log_type)
			{
			case FORMAT_DESCRIPTION_EVENT:
			{
				FORMAT_DESC_EVENT formate_desc ;
				formate_desc.encode(buffer);
				break;
			}
			case QUERY_EVENT:
			{
				TL_QUERY_EVENT query_event;
				query_event.encode(buffer, &header);
				DBG("find query event ,query statement is : %s\n",query_event.get_sql_statement());
				break;
			}
			case TABLE_MAP_EVENT:
			{
				TL_TABLE_MAP_EVENT table_map;
				table_map.encode(buffer, &header);
				DBG("find table map event ,table name is : %s\n", table_map.get_table_nmae());
				break;
			}
			case WRITE_ROWS_EVENT:
			{
				TL_ROW_EVENT row_event;
				row_event.encode(buffer, &header);
				DBG("find table row event[write] ,cloumn count is : %d\n", row_event.get_column_count());
				break;
			}
			case UPDATE_ROWS_EVENT:
			{
				TL_ROW_EVENT row_event;
				row_event.encode(buffer, &header);
				DBG("find table row event[update] ,cloumn count is : %d\n", row_event.get_column_count());
				break;
			}
			case  DELETE_ROWS_EVENT:
			{
				TL_ROW_EVENT row_event;
				row_event.encode(buffer, &header);
				DBG("find table row event[delete] ,cloumn count is : %d\n", row_event.get_column_count());
				break;
			}
			case PREVIOUS_GTIDS_LOG_EVENT:
			{
				break;
			}
			default:
				buffer->move_position(header.get_event_len());
				break;
			}
			
		}
		return this;
	}
}TL_BINUTIL,*PTL_BINUTIL;
#endif // !BINLOG_UTIL_H
